<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="资源编码" prop="sourceCode">
        <el-input
          v-model="queryParams.sourceCode"
          placeholder="请输入资源编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源名称" prop="sourceName">
        <el-input
          v-model="queryParams.sourceName"
          placeholder="请输入资源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源认证类型" prop="sourceAuthType">
        <el-select v-model="queryParams.sourceAuthType" placeholder="请选择资源认证类型" clearable>
          <el-option
            v-for="dict in dict.type.source_auth_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资源授权类型" prop="sourcePayType">
        <el-select v-model="queryParams.sourcePayType" placeholder="请选择资源授权类型" clearable>
          <el-option
            v-for="dict in dict.type.source_pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属企业" prop="belongCompanyName">
        <el-input
          v-model="queryParams.belongCompanyName"
          placeholder="请输入所属企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['net:authInfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['net:authInfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['net:authInfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['net:authInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="authInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="资源编码" align="center" width="200" prop="sourceCode"/>
      <el-table-column label="资源名称" align="center" width="200" prop="sourceName"/>
      <el-table-column label="资源认证类型" align="center" width="160" prop="sourceAuthType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.source_auth_type" :value="scope.row.sourceAuthType"/>
        </template>
      </el-table-column>
      <el-table-column label="资源认证有效期(秒)" align="center" width="200" prop="sourceAuthTime"/>
      <el-table-column label="资源授权类型" align="center" width="120" prop="sourcePayType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.source_pay_type" :value="scope.row.sourcePayType"/>
        </template>
      </el-table-column>
      <el-table-column label="资源授权积分" align="center" width="120" prop="sourceAuthFee"/>
      <el-table-column label="资源现金金额" align="center" width="120" prop="sourcePrice"/>
      <el-table-column label="是否展示插屏广告" align="center" width="140"  prop="sourceAdCp">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.all_yes_no" :value="scope.row.sourceAdCp"/>
        </template>
      </el-table-column>
      <el-table-column label="资源部门" align="center" prop="sourceDeptName"/>
      <el-table-column label="操作" align="center" width="200" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['net:authInfo:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['net:authInfo:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改认证应用（资源）信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="资源编码" prop="sourceCode">
              <el-input v-model="form.sourceCode" placeholder="请输入资源编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源名称" prop="sourceName">
              <el-input v-model="form.sourceName" placeholder="请输入资源名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源认证类型" prop="sourceAuthType">
              <el-select v-model="form.sourceAuthType" placeholder="请选择资源认证类型">
                <el-option
                  v-for="dict in dict.type.source_auth_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.sourceAuthType === '0'" :span="12">
            <el-form-item label="资源密钥" prop="sourcePassword">
              <el-input v-model="form.sourcePassword" placeholder="请输入资源密钥 "/>
            </el-form-item>
          </el-col>
          <el-col v-if="form.sourceAuthType === '2' || form.sourceAuthType === '3'" :span="12">
            <el-form-item label="资源认证有效期(秒)" prop="sourceAuthTime">
              <el-input v-model="form.sourceAuthTime" placeholder="请输入资源认证有效期(秒)  "/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源授权类型" prop="sourcePayType">
              <el-select v-model="form.sourcePayType" placeholder="请选择资源授权类型">
                <el-option
                  v-for="dict in dict.type.source_pay_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源授权积分" prop="sourceAuthFee">
              <el-input v-model="form.sourceAuthFee" placeholder="请输入资源授权积分"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源现金金额" prop="sourcePrice">
              <el-input v-model="form.sourcePrice" placeholder="请输入资源现金金额"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否展示插屏广告" prop="sourceAdCp">
              <el-switch
                v-model="form.sourceAdCp"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAuthInfo, getAuthInfo, delAuthInfo, addAuthInfo, updateAuthInfo} from "@/api/net/authInfo";

export default {
  name: "AuthInfo",
  dicts: ['source_pay_type', 'source_auth_type', 'all_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 认证应用（资源）信息表格数据
      authInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sourceCode: null,
        sourceName: null,
        sourceDept: null,
        sourceDeptName: null,
        sourceAuthType: null,
        sourcePassword: null,
        sourcePublicKey: null,
        sourcePrivateKey: null,
        sourceAuthTime: null,
        sourcePayType: null,
        sourceAuthFee: null,
        sourcePrice: null,
        sourceAdCp: null,
        belongCompany: null,
        belongCompanyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sourceCode: [
          {required: true, message: "资源编码不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询认证应用（资源）信息列表 */
    getList() {
      this.loading = true;
      listAuthInfo(this.queryParams).then(response => {
        this.authInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sourceCode: null,
        sourceName: null,
        sourceDept: null,
        sourceDeptName: null,
        sourceAuthType: null,
        sourcePassword: null,
        sourcePublicKey: null,
        sourcePrivateKey: null,
        sourceAuthTime: null,
        sourcePayType: null,
        sourceAuthFee: null,
        sourcePrice: null,
        sourceAdCp: null,
        belongCompany: null,
        belongCompanyName: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加认证应用（资源）信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAuthInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改认证应用（资源）信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuthInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuthInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除认证应用（资源）信息编号为"' + ids + '"的数据项？').then(function () {
        return delAuthInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('net/authInfo/export', {
        ...this.queryParams
      }, `authInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
